package com.ok.cartoon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ok.cartoon.domain.dto.OkCartoonDto;
import com.ok.cartoon.domain.po.OkCartoon;
import com.ok.cartoon.mapper.OkCartoonMapper;
import com.ok.cartoon.service.IOkCartoonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ok.common.domain.po.CartoonDoc;
import com.ok.common.utils.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.reflections.util.Utils;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 漫画表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-28
 */
@Slf4j
@Service
public class OkCartoonServiceImpl extends ServiceImpl<OkCartoonMapper, OkCartoon> implements IOkCartoonService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    @Transactional
    public void add(OkCartoonDto okCartoonDto) {
        OkCartoon okCartoon = new OkCartoon();
        //对象拷贝
        BeanUtil.copyProperties(okCartoonDto,okCartoon);
        //添加雪花id标识
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        long fid = idWorker.nextId();
        okCartoon.setSnowflakeId(fid);
        okCartoon.setStatus("连载中");
        this.save(okCartoon);

        LambdaQueryWrapper<OkCartoon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OkCartoon::getSnowflakeId,fid);
        OkCartoon cartoon = this.getOne(queryWrapper);
        //封装cartoonDoc对象
        CartoonDoc cartoonDoc = new CartoonDoc();
        BeanUtil.copyProperties(cartoon, cartoonDoc);
        log.info("es添加的okCartoon:{}",cartoonDoc);
        //发送消息添加es中cartoon信息
        try {
            rabbitTemplate.convertAndSend("cartoon_direct","",cartoonDoc);
        } catch (AmqpException e) {
            log.error("发送消息添加漫画es失败：{}",e.getMessage());
        }
    }

    @Override
    public Page<OkCartoon> pageWith(Integer pageNo, Integer pageSize, Integer categoryId, Integer areaId) {
        Page<OkCartoon> pageInfo = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<OkCartoon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Utils.isEmpty(String.valueOf(categoryId)),OkCartoon::getCategoryId,categoryId);
        queryWrapper.eq(Utils.isEmpty(String.valueOf(areaId)),OkCartoon::getAreaId,areaId);

        Page<OkCartoon> page = this.page(pageInfo, queryWrapper);
        return page;

    }

    @Override
    public void delete(Integer id) {
        this.removeById(id);
        //发送消息删除es漫画信息
        try {
            rabbitTemplate.convertAndSend("cartoon_direct","",id);
        } catch (AmqpException e) {
            log.error("发送消息删除漫画es失败：{}",e.getMessage());
        }
    }

    @Override
    public void updateWith(OkCartoonDto okCartoonDto) {
        OkCartoon okCartoon = new OkCartoon();
        BeanUtil.copyProperties(okCartoonDto,okCartoon);
        this.updateById(okCartoon);
        OkCartoon okCartoon1 = this.getById(okCartoonDto.getId());
        CartoonDoc cartoonDoc =BeanUtil.copyProperties(okCartoon1,CartoonDoc.class);
        //发送消息修改es漫画信息
        try {
            rabbitTemplate.convertAndSend("cartoon_direct","",cartoonDoc);
        } catch (AmqpException e) {
            log.error("发送消息删除漫画es失败：{}",e.getMessage());
        }
    }
}
